package com.tomato.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.tomato.entity.TodoRecord;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface TodoRecordMapper extends BaseMapper<TodoRecord> {
    /**
     * 查询指定用户指定时间范围内的完成记录
     * @param userId 用户ID
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 完成记录列表
     */
    @Select("SELECT * FROM todo_record " +
            "WHERE user_id = #{userId} " +
            "AND start_time >= #{startTime} AND start_time < #{endTime} " +
            "ORDER BY start_time DESC")
    List<TodoRecord> selectByUserIdAndDateRange(
            @Param("userId") Long userId,
            @Param("startTime") LocalDateTime startTime,
            @Param("endTime") LocalDateTime endTime);

    /**
     * 根据设定ID查询记录
     * @param settingId 设定ID
     * @return 记录列表
     */
    @Select("SELECT * FROM todo_record WHERE setting_id = #{settingId} ORDER BY start_time DESC")
    List<TodoRecord> selectBySettingId(@Param("settingId") Long settingId);

    /**
     * 根据用户ID删除所有记录
     * @param userId 用户ID
     * @return 删除的记录数
     */
    @Select("DELETE r.* FROM todo_record r " +
            "JOIN todo_setting s ON r.setting_id = s.id " +
            "WHERE s.user_id = #{userId}")
    Integer deleteByUserId(@Param("userId") Long userId);
}